<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Cities;
use App\Models\FilmTypeMaster;
use App\Models\MacroManagement;
use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\PartMaster;
use App\Models\PhoneExtension;
use App\Models\ProductTypes;
use App\Models\ProvinceMaster;
use App\Models\RegionMaster;
use App\Models\Warranty;
use Encore\Admin\Grid\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Lang;

class AjaxController extends Controller
{

    public function index(Request $request)
    {
        $t = $request->input('t');
        if (in_array($t, ['warranty', 'lang_change'])) {
            return $this->$t($request);
        }
        return abort(404);
    }

    public function lang_change(Request $request)
    {
        $language = (int) $request->input('lang');
        if ($language == 1) {
            App::setLocale('en');
            session(['language'=>1]);
        } elseif ($language == 2) {
            App::setLocale('zh_tw');
            session(['language'=>2]);
        } elseif ($language == 3) {
            App::setLocale('zh');
            session(['language'=>3]);
        } else {
            App::setLocale('zh');
            session(['language'=>3]);
        }
        return ['status'=>'SUCCESS'];
    }

    public function warranty(Request $request)
    {
        $a = $request->input('a');
        $warranty_id = $request->input('warranty_id');
        if ($a == 'getSmsCode') {
            $phone_number = trim($request->input('phone_number'));
            $extension = trim($request->input('extension'));
            $result = $this->checkPhone($phone_number);
            if ($result !== true) {
                return $result;
            }

            return sendSMSCode($phone_number, 1, $extension);
        } elseif ($a == 'checkSmsCode') {
            return $this->checkSmsCode($request);
        } elseif ($a == 'getWarrantyDetails') {
            // 判断warranty_id是否存在
            $model = Warranty::where('warranty_id', $warranty_id)->where('approved', 1)->first();

            if (empty($model) || empty($model->warranty_id) || empty($model->first_name)) {
                return fs404('数据没有找到');
            }

            return fs200('成功！');
        }

        $value = trim(request()->input('svalue'));
        $columns = ['warranty_id', 'phone_number', 'license_plate'];
        if (empty($value)) {
            return ['status'=>false, 'data'=>$this->mH1()];
        }
        $warranty = new Warranty();
        $warranty = $warranty->where(function ($query) use ($value, $columns) {
            foreach ($columns as $k => $c) {
                $query->orWhere($c, $value);
            }
        });
        $data = $warranty->get();
        if (!count($data)) {
            return ['status'=>false, 'data'=>$this->mH1()];
        }
        $res = [];
        foreach ($data as $k => $v) {
            $res[] = $this->mH3($v);
        }
        return ['status'=>'SUCCESS', 'data'=>$res];
        return $warranty;

    }

    public function checkPhone($phone_number)
    {
        $data = DB::table('warranty as w')
            ->where('w.phone_number', $phone_number)
            ->whereNull('w.deleted_at')
            ->where('w.warranty_id', '>=', 1)
            ->where('w.approved', 1)
            ->where('w.first_name', '!=', '0')
            ->whereNotNull('w.first_name')
            ->whereNotNull('w.license_plate')
            ->orderByDesc('w.id')
            ->select(['w.phone_number'])
            ->first();

        if (empty($data) || empty($data->phone_number)) {
            return fs403('手机号对应信息不存在');
        }

        return true;
    }

    /**
     * 校验短信验证
     *
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse|void
     */
    public function checkSmsCode(Request $request)
    {
        $phone_number = trim($request->input('phone_number'));
        $sms_code = trim($request->input('sms_code'));
        $extension =trim($request->input('extension'));
        $type = 1;

        $result = $this->checkPhone($phone_number);
        if ($result !== true) {
            return $result;
        }

        $bool = checkSmsCode($phone_number, $type, $sms_code, $extension);

        if (@$bool['status'] == 'SUCCESS') {
            return $bool;
        }

        return fs403(@$bool['msg'], $bool, [$phone_number, $type, $sms_code, $extension]);
    }

    protected function mH3($warranty)
    {
        $res = [
            [
                'value' => ts('VALIDWARRANTYID'),
            ],
            [
                'label' => ts('WARRANTYID'),
                'value' => $warranty->warranty_id,
            ],
            [
                'label' => ts('CUSTOMERNAME'),
                'value' => $warranty->first_name,
            ],
            [
                'label' => ts('MAKE'),
                'value' => ts('make_maser.'.$warranty->make_id),
            ],
            [
                'label' => ts('MODEL'),
                'value' => $warranty->model_id,
            ],
            [
                'label' => ts('YEAR'),
                'value' => $warranty->year_id,
            ],
            [
                'label' => ts('INSTALLATIONDATE'),
                'value' => $warranty->installation_date,
            ],
            [
                'label' => ts('COMPANYNAME'),
                'value' => $warranty->company_name,
            ],
            [
                'label' => ts('CITY'),
                'value' => ts('cities.'.$warranty->make_id),
            ]
        ];
        return $res;
    }

    protected function mH2($warranty)
    {
        $item = '<table class="table table-bordered">';
        $item .= '<tbody>';
        $item .= '<tr><td colspan="2">'.ts('VALIDWARRANTYID').'</td></tr>';
        $item .= '</tbody>';
        $item .= '</table>';
        return $item;
    }

    protected function mH1()
    {
        $msg = '';
        $msg .= "<p>".ts('WERRORLINE1')."</p>";

        return $msg;

    }

}
